<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/19 0019
 * Time: 8:59
 */

namespace app\admin\controller;

use think\Controller;
class Eat extends Controller
{
    public function index(){
        $data = db("eat")->select();
        $this->assign("data",$data);
        return view("index");
    }
    public function add(){
        if(request()->post()){
            $param = request()->except("image");
           $file = request()->file('image');
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
            $data = [
                'type' => $param['type'],
                'title' => $param['title'],
                'descr' => $param['descr'],
                'update' => date("Y-m-d H:i:s",time()),
                'img' => $image,
            ];
            $res = db("eat")->insert($data);
            if($res){
                return 200;
            }else{
                return -1;
            }
        }
       return view("add");
    }
    public function insert(){
        $param = request()->except("image");
        $file = request()->file('image');
        $info=$file->move( './static/images');
        $filename=str_replace("\\","/",$info->getSaveName());
        $image = '/static/images/'.$filename;
        $data = [
            'img' => $image,
            'update' => date("Y-m-d H:i:s"),
            'title' => $param['title'],
            'descr' => $param['descr'],
            'type' => $param['type'],
        ];
//        return $data;
        $res = db("eat")->insert($data);
        if($res){
            return 200;
        }else{
            return -1;
        }
    }
}